package com.cheung.leetcode.dp;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.dp
 * @Author :  cheungming
 * @CreateTime : 2024-06-17 09:03:47
 * @Description : 单词拆分
 * @Version : 1.0
 */
public class Code139Test {

    public boolean wordBreak(String s, List<String> wordDict) {
        // 本题可以将wordDict看作物品,s为背包,背包容量为s.length()
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 1; i <= s.length(); i++) {
            //
            for (int j = 0; j < i && !dp[i]; j++) {
                if (dp[j] && wordDict.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }

        return dp[s.length()];
    }

    @Test
    public void test1() {
        assert wordBreak("leetcode", Arrays.asList("leet", "code"));
    }

    @Test
    public void test2() {
        assert wordBreak("applepenapple", Arrays.asList("apple", "pen"));
    }

    @Test
    public void test3() {
        assert !wordBreak("catsandog", Arrays.asList("cats", "dog", "sand", "and", "cat"));
    }
}
